import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Arrays;

/**
 * 骨牌类
 */
public class Dominoes extends JLabel implements MouseListener {
    /**
     * 骨牌点数
     */
    int[] point = new int[2];
    /**
     * 骨牌宽度
     */
    int w = 75;
    /**
     * 骨牌
     */
    int h = 150;
    /**
     * 后缀
     */
    String suffix = ".png";
    /**
     * 骨牌名
     */
    String name;
    /**
     * 骨牌贴图路径
     */
    String Path;
    boolean isClicked = false;/*是否被点击过*/
    /**
     * 是否为双牌
     */
    boolean isDouble = false;


    /**
     * 通过牌名生成实例
     * @param name 骨牌名称
     */
    public Dominoes(String name){
        this.name = name;
        point[0] = name.charAt(0) - '0';
        point[1] = name.charAt(1) - '0';
        if(point[0] == point[1])isDouble = true;
        //System.out.println(point[0]+" "+point[1]);
        Path = "./imageSrc/"+name+suffix;
        Icon icon = new ImageIcon(Path);
        this.setSize(w,h);
        this.setIcon(icon);
      //  System.out.println("create"+name);
    }
    @Override
    public void mouseClicked(MouseEvent e) {

        boolean preStatus = isClicked;
        isClicked = !isClicked;//实现点击选中，再点击取消选中
        if(!preStatus){//没选中到被选中
           // System.out.println("selected "+this.name);//Db
            // 其他牌落下
            if(MyLibrary.SelectedDominoes != null){
                int x = MyLibrary.SelectedDominoes.getX();
                MyLibrary.SelectedDominoes.setLocation(x,30);
                MyLibrary.SelectedDominoes.isClicked = false;
               // System.out.println("unselected "+MyLibrary.SelectedDominoes.name);
            }
            MyLibrary.SelectedDominoes = this;

        }else{//反之
            MyLibrary.SelectedDominoes  = null;
            //System.out.println("unselected "+this.name);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {

        int preX;
        preX = getX();
       if(!isClicked) this.setLocation(preX,10);


    }

    @Override
    public void mouseExited(MouseEvent e) {

        int preX;
        preX = getX();
        //System.out.println("released");
        if(!isClicked)this.setLocation(preX,30);


    }
}
